<?php
class DelegatorReciboCliente extends DelegatorPrueba
{
	public function __construct() {}
	public function verificacion(EFacade & $facade,$transSet) 
	{
		
		//$this->porDepartamento($facade,$transSet);
		//echo 'aki';
		$ezpdo_ = & getManager();
		$queryDep='FROM MVariant as t WHERE t.identificadorUnico=?';
		$doc = & $facade->getDocumentRoot();
		//$usuarioActual=$doc->usuarioActual;
		$idDep=$doc->departamento->identificadorUnico;
		$variante=$doc->variante;
		$workflow= & $facade->getWorkflow();
		$areaTrabajo=NULL;
		$role=NULL;
		$cont=0;
	    $currentTransSet=$facade->getCurrentTransSet($workflow,$role);
		$currentPlaceSet=$facade->getCurrentPlaceSet($workflow);
		$dptoVarietyList=$currentPlaceSet->dptoVarietyList;
		
		foreach($dptoVarietyList as $dv){
			
			foreach($dv->variantList as $v){
				if($v->epGetObjectId()==$variante->epGetObjectId()){
					
					
					$departamento1=$ezpdo_->find($queryDep,$dv->dptoName);
					$doc->departamento=$departamento1[0];
					$doc->variante=$v;
				}
			}
			
		}
		
		$role=$currentTransSet->transList[0]->roleRef;
		//echo '<br>departamento',$doc->departamento;
		
		$query = 'FROM MUser as u WHERE u.perfilList.contains(p) AND p.roleList.contains(?) AND u.empleado.departamento=?';
			$users = $ezpdo_->find($query, $role,$doc->departamento);
		
			//echo '<br>XX',$users[0];
		//$users = $session->find($query, $role,$areaTrabajo);
		$ret=NULL;
		foreach ($users as & $user) {
			if ($ret == null) {
				$ret = & $user;
			}

			if ($user->getActorList()->count() < $ret->getActorList()->count()) {
				$ret = & $user;
			}
		}
		//echo '<br>--.XX ',$ret;
//		$workTransList= & $workflow->workTransList;
//		$transList=$currentTransSet->transList;
//		$wTransList=array();
//		$actorList=array();
		
//		foreach($workTransList as &$wt){
//			echo '<br> WT--> ',$wt;
//			foreach($transList as $t){
//				echo '<br> T--> ',$t;
//				$cont = 0;
//				if($wt->transRef->epGetObjectId()==$t->epGetObjectId()){
//					if($workflow->epGetObjectId() != NULL){
//						
//						echo '<br> esto ',$query1 = 'FROM MActor as a WHERE a.workflowRef.oid=? AND a.roleRef.oid=?';
//						$actores = $ezpdo_->find($query1,$workflow->epGetObjectId(), $role->epGetObjectId());
//						
//						if(count($actores) == 0){
//							$cont=1;
//						}
//					}
//					else{
//						$cont=1;
//					}
					
					//echo '<br> esto ',$query1 = 'FROM MActor as a WHERE a.workflowRef.oid=? AND a.roleRef.oid=?';
					//$actores = $ezpdo_->find($query1,$workflow->epGetObjectId(), $role->epGetObjectId());
					
//					echo '<br>--.ññ ',count($actores);
//					echo '<br>-- ',$actores[0];
//					if($cont == 1){
//						$actor = $facade->session->create('MActor');
//						$actor->userRef=$ret;
//						$actor->roleRef = $role;
//						$actor->workflowRef = $workflow;
//					}
//					else{
//						$actor = $actores[0];
//					}
//					
//					$actor->workTransList[] = $wt;
//					$actor->commit();
					
//				}
//				
//			}
//			
//		}
		
		$query123 = 'FROM MActor as u WHERE u.userRef.oid=?';
		$actores = $ezpdo_->find($query123, $ret->epGetObjectId());
			
		
		if(!count($actores)){
			$actor = $facade->session->create('MActor');
			$actor->userRef=$ret;
			$actor->roleRef = $role;
			$actor->workflowRef = $workflow;
		}
		else{
			$actor = $actores[0];
		}
		//echo '<br>cooooo ',$actor;
		//$actor->commit();	
		$workTransList= & $workflow->workTransList;
		$transList=$currentTransSet->transList;
		foreach($workTransList as &$wt){
			//echo '<hr> WT--> ',$wt;
			foreach($transList as $t){
				//echo '<br> T--> ',$t;
				if($wt->transRef->epGetObjectId()==$t->epGetObjectId()){
					$wt->actorRef = $actor;
					//$wt->actorRef->userRef=$ret;
					//$wt->commit();
					//echo '<br>33--. ',$wt,'<br> -- ',$wt->actorRef;
					
				}
			}
		}
	}
	
	public function agente(EFacade & $facade,$transSet) 
	{
		$ezpdo_ = & getManager();
		$queryDep='FROM MVariant as t WHERE t.identificadorUnico=?';
		$doc = & $facade->getDocumentRoot();
		//$usuarioActual=$doc->usuarioActual;
		$idDep=$doc->departamento->identificadorUnico;
		$variante=$doc->variante;
		$workflow= & $facade->getWorkflow();
		$areaTrabajo=NULL;
		$role=NULL;
		$cont=0;
	    $currentTransSet=$facade->getCurrentTransSet($workflow,$role);
		$currentPlaceSet=$facade->getCurrentPlaceSet($workflow);
		$dptoVarietyList=$currentPlaceSet->dptoVarietyList;
		
		foreach($dptoVarietyList as $dv){
			
			foreach($dv->variantList as $v){
				if($v->epGetObjectId()==$variante->epGetObjectId()){
					
					
					$departamento1=$ezpdo_->find($queryDep,$dv->dptoName);
					$doc->departamento=$departamento1[0];
					$doc->variante=$v;
				}
			}
			
		}
		
		$role=$currentTransSet->transList[0]->roleRef;
		//echo '<br>departamento',$doc->departamento;
		
		$query = 'FROM MUser as u WHERE u.perfilList.contains(p) AND p.roleList.contains(?) AND u.empleado.departamento=?';
			$users = $ezpdo_->find($query, $role,$doc->departamento);
		
			//echo '<br>XX',$users[0];
		//$users = $session->find($query, $role,$areaTrabajo);
		$ret=NULL;
		foreach ($users as & $user) {
			if ($ret == null) {
				$ret = & $user;
			}

			if ($user->getActorList()->count() < $ret->getActorList()->count()) {
				$ret = & $user;
			}
		}
		//echo '<br>--.XX ',$ret;
//		$workTransList= & $workflow->workTransList;
//		$transList=$currentTransSet->transList;
//		$wTransList=array();
//		$actorList=array();
		
//		foreach($workTransList as &$wt){
//			echo '<br> WT--> ',$wt;
//			foreach($transList as $t){
//				echo '<br> T--> ',$t;
//				$cont = 0;
//				if($wt->transRef->epGetObjectId()==$t->epGetObjectId()){
//					if($workflow->epGetObjectId() != NULL){
//						
//						echo '<br> esto ',$query1 = 'FROM MActor as a WHERE a.workflowRef.oid=? AND a.roleRef.oid=?';
//						$actores = $ezpdo_->find($query1,$workflow->epGetObjectId(), $role->epGetObjectId());
//						
//						if(count($actores) == 0){
//							$cont=1;
//						}
//					}
//					else{
//						$cont=1;
//					}
					
					//echo '<br> esto ',$query1 = 'FROM MActor as a WHERE a.workflowRef.oid=? AND a.roleRef.oid=?';
					//$actores = $ezpdo_->find($query1,$workflow->epGetObjectId(), $role->epGetObjectId());
					
//					echo '<br>--.ññ ',count($actores);
//					echo '<br>-- ',$actores[0];
//					if($cont == 1){
//						$actor = $facade->session->create('MActor');
//						$actor->userRef=$ret;
//						$actor->roleRef = $role;
//						$actor->workflowRef = $workflow;
//					}
//					else{
//						$actor = $actores[0];
//					}
//					
//					$actor->workTransList[] = $wt;
//					$actor->commit();
					
//				}
//				
//			}
//			
//		}
		
		$query123 = 'FROM MActor as u WHERE u.userRef.oid=?';
		$actores = $ezpdo_->find($query123, $ret->epGetObjectId());
			
		
		if(!count($actores)){
			$actor = $facade->session->create('MActor');
			$actor->userRef=$ret;
			$actor->roleRef = $role;
			$actor->workflowRef = $workflow;
		}
		else{
			$actor = $actores[0];
		}
		//echo '<br>cooooo ',$actor;
		//$actor->commit();	
		$workTransList= & $workflow->workTransList;
		$transList=$currentTransSet->transList;
		foreach($workTransList as &$wt){
			//echo '<hr> WT--> ',$wt;
			foreach($transList as $t){
				//echo '<br> T--> ',$t;
				if($wt->transRef->epGetObjectId()==$t->epGetObjectId()){
					$wt->actorRef = $actor;
					//$wt->actorRef->userRef=$ret;
					//$wt->commit();
					//echo '<br>33--. ',$wt,'<br> -- ',$wt->actorRef;
					
				}
			}
		}
		//echo '<br>11--. ',$wt->actorRef;
		//echo '<br>55--. ',$wt;
	}
	public function generaRecibo(EFacade & $facade,$transSet) 
	{	
//		$ezpdo_ = & getManager();
//		$queryDep='FROM MDepartamento as t WHERE t.identificadorUnico=?';
//		$doc = & $facade->getDocumentRoot();
//		$usuarioActual=$doc->usuarioActual;
//		$idDep=$doc->departamento->identificadorUnico;
//		$variante=$doc->variante;
//		$workflow= & $facade->getWorkflow();
//		$areaTrabajo=NULL;
//		$role=NULL;
//		$cont=0;
//	    $currentTransSet=$facade->getCurrentTransSet($workflow,$role);
//		$currentPlaceSet=$facade->getCurrentPlaceSet($workflow);
//		$dptoVarietyList=$currentPlaceSet->dptoVarietyList;
//		foreach($dptoVarietyList as $dv){
//			
//			foreach($dv->variantList as $v){
//				if($v->epGetObjectId()==$variante->epGetObjectId()){
//					
//					
//					$departamento1=$ezpdo_->find($queryDep,$dv->dptoName);
//					$doc->departamento=$departamento1[0];
//					$doc->variante=$v;
//				}
//			}
//			
//		}
//		$role=$currentTransSet->transList[0]->roleRef;
//		//echo '<br>departamento',$doc->departamento;
//		
//		$query = 'FROM MUser as u WHERE u.perfilList.contains(p) AND p.roleList.contains(?) AND u.empleado.departamento=?';
//			$users = $ezpdo_->find($query, $role,$doc->departamento);
//		
//			
//		//$users = $session->find($query, $role,$areaTrabajo);
//		$ret=NULL;
//		foreach ($users as & $user) {
//			if ($ret == null) {
//				$ret = & $user;
//			}
//
//			if ($user->getActorList()->count() < $ret->getActorList()->count()) {
//				$ret = & $user;
//			}
//		}
//		
//		$workTransList= & $workflow->workTransList;
//		$transList=$currentTransSet->transList;
//		foreach($workTransList as $wt){
//			
//			foreach($transList as $t){
//				
//				if($wt->transRef->epGetObjectId()==$t->epGetObjectId()){
//					
//					$wt->actorRef->userRef=$ret;
//				}
//			}
//		}
//		
		
	}
}
?>